// 创建axios实例
import axios from "axios";

// 请求示例：https://restapi.amap.com/v3/ip?ip=114.247.50.2&key=da31338ed254c1cdbc3c43a6e6742f0e
// key:da31338ed254c1cdbc3c43a6e6742f0e
const instance = axios.create({
    baseURL: "https://restapi.amap.com/v3",
    timeout: 5000,
});

// 添加请求拦截器
instance.interceptors.request.use(
    (config) => {
        config.params = {
            ...config.params,
            key: "da31338ed254c1cdbc3c43a6e6742f0e",
        };
        return config;
    },
    (error) => Promise.reject(error)
);

// 添加响应拦截器
instance.interceptors.response.use(
    (response) => {
        if (response.data.status !== "1") {
            // 高德API成功状态码为字符串"1"
            throw new Error(`API错误：${response.data.info}`);
        }
        return response.data;
    },
    (error) => {
        console.error("请求失败:", error.message);
        return Promise.reject(error);
    }
);

export default instance;
